package com.institute.entity.dto;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.institute.entity.Institute;
import com.staff.entity.Staff;

public class PrincipalDTO {
	private Long id;
	private String staffName;
	private String staffNum;
	private String position;
	private Long deptId;
	
	
	public PrincipalDTO(Staff staff,Long deptId,String position) {
		super();
		this.id = staff.getId();
		this.staffName = staff.getStaffName();
		this.staffNum = staff.getStaffNum();
		this.position = position;
		this.deptId = deptId;
	}
	public PrincipalDTO() {
		super();
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getStaffName() {
		return staffName;
	}
	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}
	public String getStaffNum() {
		return staffNum;
	}
	public void setStaffNum(String staffNum) {
		this.staffNum = staffNum;
	}
	public String getPosition() {
		return position;
	}
	public void setPosition(String position) {
		this.position = position;
	}
	public Long getDeptId() {
		return deptId;
	}
	public void setDeptId(Long deptId) {
		this.deptId = deptId;
	}
	public static Specification<Staff> getSpecification( final Institute dept,PrincipalDTO dto)
	{
		Specification<Staff> spec = new Specification<Staff>() {
			public Predicate toPredicate(Root<Staff> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				
				 if(null != dept) {
					 Predicate  p =  cb.equal(root.get("department").as(Institute.class),dept);
					 list.add(p);
				 }
				 if(null != dto && !StringUtils.isEmpty(dto.getStaffName())) {
					 Predicate  p =  cb.like(root.get("staffName").as(String.class),"%"+ dto.getStaffName() + "%");
					 list.add(p);
				 }
				 if(null != dto && !StringUtils.isEmpty(dto.getStaffNum())) {
					 Predicate  p =  cb.like(root.get("staffNum").as(String.class),"%"+ dto.getStaffNum() + "%");
					 list.add(p);
				 }
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
}
